package com.dg.cuckoo.common.utils;

import com.dg.cuckoo.admin.dto.basicInformation.projectInformation.BasicProjectDto;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

public class ExcelUtil {

    public static void write(List<BasicProjectDto> list, String path) throws IOException {
        XSSFWorkbook workbook = new XSSFWorkbook();

        XSSFSheet sheet = workbook.createSheet("信息表");

        XSSFRow row = sheet.createRow(0);
        row.createCell(0).setCellValue("项目代码");
        row.createCell(1).setCellValue("项目名称");
        row.createCell(2).setCellValue("总投资");
        row.createCell(3).setCellValue("赋码时间");
        row.createCell(4).setCellValue("项目注册资金");
        row.createCell(5).setCellValue("详细建设地址");
        row.createCell(6).setCellValue("国际行业");
        row.createCell(7).setCellValue("建设性质");
        row.createCell(8).setCellValue("建设规模及内容");
        row.createCell(9).setCellValue("拟开工时间");
        row.createCell(10).setCellValue("拟建成时间");
        row.createCell(11).setCellValue("总用地面积（亩）");
        row.createCell(12).setCellValue("新增建设用地（亩）");
        row.createCell(13).setCellValue("总建设面积（平方米）");
        row.createCell(14).setCellValue("地上建设面积（平方米)");
        row.createCell(15).setCellValue("项目单位名称");
        row.createCell(16).setCellValue("项目单位证照号码");
        row.createCell(17).setCellValue("项目负责人");
        row.createCell(18).setCellValue("负责人电话");
        row.createCell(19).setCellValue("项目联系人");
        row.createCell(20).setCellValue("联系人电话");
        row.createCell(21).setCellValue("项目套餐");



        list=checkList(list);
        for (int i=0;i<list.size();i++){
            XSSFRow row1 = sheet.createRow(i + 1);//从第二行开始
//            row1.setRowStyle(numStyle);
            row1.createCell(0).setCellValue(list.get(i).getProjectCode());
            row1.createCell(1).setCellValue(list.get(i).getMainName());
            row1.createCell(2).setCellValue(list.get(i).getTotalInvestment());
            row1.createCell(3).setCellValue(list.get(i).getCodingTime());
            row1.createCell(4).setCellValue(list.get(i).getProjectRegisteredCapital());
            row1.createCell(5).setCellValue(list.get(i).getDetailedAddress());
            row1.createCell(6).setCellValue(list.get(i).getNationalStandardIndustry());
            row1.createCell(7).setCellValue(list.get(i).getConstructive());
            row1.createCell(8).setCellValue(list.get(i).getConstructionContent());
            row1.createCell(9).setCellValue(list.get(i).getProposedStartTime());
            row1.createCell(10).setCellValue(list.get(i).getProposedCompletionTime());
            row1.createCell(11).setCellValue(list.get(i).getTotalLandArea());
            row1.createCell(12).setCellValue(list.get(i).getConstructionLand());
            row1.createCell(13).setCellValue(list.get(i).getTotalLandArea());
            row1.createCell(14).setCellValue(list.get(i).getFloorArea());
            row1.createCell(15).setCellValue(list.get(i).getProjectUnitName());
            row1.createCell(16).setCellValue(list.get(i).getProjectUnitNumber());
            row1.createCell(17).setCellValue(list.get(i).getProjectManager());
            row1.createCell(18).setCellValue(list.get(i).getPrincipalPhone());
            row1.createCell(19).setCellValue(list.get(i).getProjectContact());
            row1.createCell(20).setCellValue(list.get(i).getProjectPhone());
            row1.createCell(21).setCellValue(list.get(i).getProjectPackage());
        }
        FileOutputStream outputStream = new FileOutputStream(path);
        workbook.write(outputStream);
        outputStream.flush();
        outputStream.close();
    }

    public static List<BasicProjectDto> checkList(List<BasicProjectDto> list){
        for(BasicProjectDto l:list){
            if(l.getProjectCode()==null){
                l.setProjectCode("");
            }
            if(l.getMainName()==null){
                l.setMainName("");
            }
            if(l.getTotalInvestment()==null){
                l.setTotalInvestment("");
            }
            if(l.getCodingTime()==null){
                l.setCodingTime("");
            }
            if(l.getProjectRegisteredCapital()==null){
                l.setProjectRegisteredCapital("");
            }
            if(l.getDetailedAddress()==null){
                l.setDetailedAddress("");
            }
            if(l.getNationalStandardIndustry()==null){
                l.setNationalStandardIndustry("");
            }
            if(l.getConstructive()==null){
                l.setConstructive(0);
            }
            if(l.getConstructionContent()==null){
                l.setConstructionContent("");
            }
            if(l.getProposedStartTime()==null){
                l.setProposedStartTime("");
            }
            if(l.getProposedCompletionTime()==null){
                l.setProposedCompletionTime("");
            }
            if(l.getTotalLandArea()==null){
                l.setTotalLandArea("");
            }
            if(l.getConstructionLand()==null){
                l.setConstructionLand("");
            }
            if(l.getTotalLandArea()==null){
                l.setTotalLandArea("");
            }
            if(l.getFloorArea()==null){
                l.setFloorArea("");
            }
            if(l.getProjectUnitName()==null){
                l.setProjectUnitName("");
            }
            if(l.getProjectUnitNumber()==null){
                l.setProjectUnitNumber("");
            }
            if(l.getProjectManager()==null){
                l.setProjectManager("");
            }
            if(l.getPrincipalPhone()==null){
                l.setPrincipalPhone("");
            }
            if(l.getProjectContact()==null){
                l.setProjectContact("");
            }
            if(l.getProjectPhone()==null){
                l.setProjectPhone("");
            }
            if(l.getProjectPackage()==null){
                l.setProjectPackage("");
            }
        }
        return list;
    }

}
